import functools

from django.shortcuts import render

from qn_web.models import Plates, News
from . import models


# Create your views here.

def base_con(function):  # 没用上
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        _method = function(*args, **kwargs)
        # print(dir(_method))
        # print(123123123)
        # print(_method.flush)
        # context = {}
        # context['plates'] = models.Plates.objects.all().order_by('pk')
        # context['phone'] = models.SettingWeb.objects.all()
        # context['footers'] = models.Footer.objects.all().order_by('pk')
        return _method

    return wrapper


def base_contents():
    context = {}
    context['plates'] = models.Plates.objects.all().order_by('z_index', 'pk')
    context['sw'] = models.SettingWeb.objects.filter(phone__isnull=False).first
    context['footers'] = models.Footer.objects.all().order_by('pk')
    return context

def base_sec_contents():
    context = base_contents()
    context['fly'] = models.News.objects.filter(tag='1').order_by('pk')
    return context


@base_con
def web_index(request):
    """
    主页有关
    :param request:
    :return:
    """
    context = base_contents()
    context['carousel_figures'] = models.News.objects.filter(tag='0')
    context['left_new'] = models.Plates.objects.get(show_index='1').has_news.all().first()
    context['right_news'] = models.Plates.objects.get(show_index='2').has_news.all()[:3]
    context['logo_urls'] = models.SettingWeb.objects.filter(phone__isnull=True)
    return render(request, 'web/index.html', context)


def web_list(request):
    context = base_contents()

    return render(request, 'web/wzList.html', context)


def base_sec(request):
    context = base_contents()
    context['fly'] = models.News.objects.filter(tag='1').order_by('pk')
    return render(request, 'web/base_seconds.html', context)


def secondary_page(request, pt_id=None):
    context = base_sec_contents()
    plate = Plates.objects.get(id=int(pt_id))
    context['plate'] = plate
    context['pt_id'] = pt_id
    module = plate.module.url
    return render(request, module, context)


def detail_page(request, news_id=None):
    new = News.objects.get(id=news_id)
    context = base_sec_contents()
    context['new'] = new
    return render(request, 'web/gaikuang.html', context)